package com.hp.bon.sgw.core;

import com.hp.bon.sgw.Constants;
import com.hp.bon.sgw.bean.CapabilityCtrlBean;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.core.channel.handler.IAsynCallHandler;
import com.hp.bon.sgw.core.processor.Processor;
import com.hp.bon.sgw.domain.BusinessRecord;
import com.hp.bon.sgw.domain.Node;
import com.hp.bon.sgw.service.ConfigService;
import com.hp.bon.sgw.util.dcc.DccAvpHelper;
import com.hp.ocs.diameter.message.Message;

public class CallParam {

	private String bussinessCallId;
	/**
	 * 消息ID,不完全等同SessionId
	 */
	protected String messageUniqueID;
	private String sessionId;
	private String serviceContextId;
	private int signType;
	private Object request;// Message,XMLMessage
	private Object response;// Message,XMLMessage
	private Object sendRequest;// Message,XMLMessage
	private Object recvResponse;// Message,XMLMessage

	private IAsynCallHandler asynHandler;
	private Processor processor;

	/**
	 * 请求端UID
	 */
	private String userName;
	private Node fromNode;
	private Node toNode;
	private String capability;

	private CapabilityCtrlBean capabilityBean;

	 public Processor getProcessor()
	 {
		return processor;
	 }

	 public void setProcessor(Processor processor) {
	   this.processor = processor;
	 }


	private int retCode = Constants.ResultCode.SUCCESS;

	private int serviceResultCode = -1;

	/**
	 * 节点内部对于该消息处理结果的一个说明，用于记录用具体处理逻辑和业务调用相关的消息处理说明，
	 * 比如是否通过验证、比如转换出错等等信息，需要注意，这个信息不要过长，能说明问题即可
	 */
	private String resultMsg;

	public CallParam(String sessionId, String serviceContextId, Object request, String userName, IAsynCallHandler asynHandler) {
		this.sessionId = sessionId;
		this.serviceContextId = serviceContextId;
		this.request = request;
		this.userName = userName;
		this.asynHandler = asynHandler;
		capability = serviceContextId.substring(0, serviceContextId.indexOf('@'));
	 	bussinessCallId = BusinessRecord.getSynSeq();

		if (request instanceof XmlMessage) {
			this.messageUniqueID = this.sessionId;
		} else if (request instanceof Message) {
			this.messageUniqueID = DccAvpHelper.getMessageUniqueID(this.sessionId, (Message) request);
		}
	}

	public String getSessionId() {
		return sessionId;
	}

	public String getServiceContextId() {
		return serviceContextId;
	}

	public Object getRequest() {
		return request;
	}

	public IAsynCallHandler getAsynHandler() {
		return asynHandler;
	}

	public String getBussinessCallId() {
		return bussinessCallId;
	}

	public String setBussinessCallId(String bussinessCallId) {
		return this.bussinessCallId = bussinessCallId;
	}

	public void setRequest(Object request) {
		this.request = request;
	}

	public Node getFromNode() {
		return fromNode;
	}

	public void setFromNode(Node fromNode) {
		this.fromNode = fromNode;
	}

	public int getSignType() {
		return signType;
	}

	public void setSignType(int signType) {
		this.signType = signType;
	}

	public String getUserName() {
		return userName;
	}

	public Object getResponse() {
		return response;
	}

	public void setResponse(Object response) {
		this.response = response;
	}

	public int getRetCode() {
		return retCode;
	}

	public void setRetCode(int retCode) {
		this.retCode = retCode;
	}

	public String getResultMsg() {
		return resultMsg;
	}

	public void setResultMsg(String resultMsg) {
		this.resultMsg = resultMsg;
	}

	public Node getToNode() {
		return toNode;
	}

	public void setToNode(Node toNode) {
		this.toNode = toNode;
	}

	public String getCapability() {
		return capability;
	}

	public void setServiceContextId(String serviceContextId) {
		this.serviceContextId = serviceContextId;
	}

	public long getTimeOutTime() {
		return timeOutTime;
	}

	public void setTimeOutTime(long timeOutTime) {
		this.timeOutTime = timeOutTime;
	}

	/**
	 * 正在处理中
	 */
	public static final int MSG_INPROCESS = 0;

	/**
	 * 待发送请求消息 消息处理完成，等待发送给请求节点
	 */
	public static final int MSG_WAIT_SEND_REQ = 1;

	/**
	 * 待发送请求消息 消息处理完成，等待发送给请求节点
	 */
	public static final int MSG_WAIT_SEND_ANSW = 2;
	/**
	 * 请求消息发送失败
	 */
	public static final int REQ_SEND_FAILED = 3;

	/**
	 * 等待答复 已发送的请求消息等待答复
	 */
	public static final int REQ_WAIT_RESPONSE = 4;

	/**
	 * 消息发送中 正在发送 消息无论是Outgoing的请求消息还是Outgoing的响应消息
	 */
	public static final int MSG_SENDING = 5;
	/**
	 * 消息发送失败
	 */
	public static final int MSG_SEND_FAILED = 6;

	/**
	 * 已成功完成
	 */
	public static final int REQ_SUC = 8;

	/**
	 * 异常失败完成
	 */
	public static final int REQ_ERROR = 9;

	/**
	 * 未响应 如超时等
	 */
	public static final int REQ_NORESPONE = 10;

	private long timeOutTime;

	public CapabilityCtrlBean getCapabilityBean() {
		return capabilityBean;
	}

	public void setCapabilityBean(CapabilityCtrlBean capabilityBean) {
		this.capabilityBean = capabilityBean;
	}

	public Object getSendRequest() {
		return sendRequest;
	}

	public Object getRecvResponse() {
		return recvResponse;
	}

	public void setSendRequest(Object sendRequest) {
		this.sendRequest = sendRequest;
	}

	public void setRecvResponse(Object recvResponse) {
		this.recvResponse = recvResponse;
	}

	// private long recvReqTime;// IChannel.handleRequest记录时间
	// private long sendReqTime;// IChannel.sendRequest(CallParam callParam)记录时间
	// private long recvRespTime;// IChannel.handleAnswer记录时间
	// private long sendRespTime;// IAsynCallHandler.onCallResult(CallParam
	// callParam)记录时间；
	// 对于中转/内部发起的业务请求，需要在Processor.endProcess(CallParam
	// callParam)时入库，一般在IAsynCallHandler.onCallResult之后入库
	// 中转请求fromNode和toNode都存在；内部发起的请求则只有toNode
	public long getRecvReqTime() {
		if (request == null) {
			return 0L;
		}
		if (request instanceof XmlMessage) {
			return ((XmlMessage) request).getMsgTime();
		} else if (request instanceof Message) {
			return ((Message) request)._rt0;
		}
		return -1L;
	}

	public long getSendReqTime() {
		if (sendRequest == null) {
			return 0L;
		}
		if (sendRequest instanceof XmlMessage) {
			return ((XmlMessage) sendRequest).getMsgTime();
		} else if (sendRequest instanceof Message) {
			return ((Message) sendRequest)._st9;
		}
		return -1L;
	}

	public long getRecvRespTime() {
		if (recvResponse == null) {
			return 0L;
		}
		if (recvResponse instanceof XmlMessage) {
			return ((XmlMessage) recvResponse).getMsgTime();
		} else if (recvResponse instanceof Message) {
			return ((Message) recvResponse)._rt0;
		}
		return -1L;
	}

	public long getSendRespTime() {
		if (response == null) {
			return 0L;
		}
		if (response instanceof XmlMessage) {
			return ((XmlMessage) response).getMsgTime();
		} else if (response instanceof Message) {
			return ((Message) response)._st9;
		}
		return -1L;
	}

	public long getInQueueTime() {
		return inQueueTime;
	}

	public void setInQueueTime(long inQueueTime) {
		this.inQueueTime = inQueueTime;
	}

	private long inQueueTime;

	public int getServiceResultCode() {
		return serviceResultCode;
	}

	public void setServiceResultCode(int serviceResultCode) {
		this.serviceResultCode = serviceResultCode;
	}

	public String getMessageUniqueID() {
		return messageUniqueID;
	}

	public void setMessageUniqueID(String messageUniqueID) {
		this.messageUniqueID = messageUniqueID;
	}
}
